﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace LFMTest
{
    public class Helpers
    {
        public static App getApp()
        {
            return (App)App.Current;
        }

        public async static Task<Boolean> CheckArtistInCollection(String e)
        {
            Boolean result = false;
            await Task.Run(() =>
                {
                    try
                    {
                        while (!getApp().isLibraryLoaded) this.Wait(100);
                        MusicStorageFile file = getApp().musicLibrary.Where(m => m.Artist.ToLower() == e.ToLower()).FirstOrDefault();
                        if (file != null) result = true;
                    }
                    catch { }
                });

            return result;
        }

        public async static Task<List<MusicStorageFile>> GetAlbums(String e)
        {
            List<MusicStorageFile> Albums = new List<MusicStorageFile>();

            //await Task.Run(() =>
            //    {
                    var albums = getApp().musicLibrary.Where(m => m.Artist.ToLower() == e.ToLower());
                    while (!getApp().isLibraryLoaded) Thread.Sleep(100);
                    foreach (var s in albums)
                    {
                        Boolean exists = false;
                        foreach (var a in Albums) if (s.Album == a.Album) exists = true;
                        if (!exists)
                        {
                            //var thumb = await s.File.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.MusicView);
                            //BitmapImage bmp = new BitmapImage();
                            //bmp.CreateOptions = BitmapCreateOptions.BackgroundCreation;
                            //bmp.DecodePixelHeight = 150;
                            //bmp.DecodePixelWidth = 150;
                            //InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
                            //RandomAccessStream.Copy(thumb.GetInputStreamAt(0), ras.GetOutputStreamAt(0));
                            //bmp.SetSource((IRandomAccessStream)ras);
                            //s.Thumbnail = bmp;
                            Albums.Add(s);
                        }
                    }
                //});

            return Albums;
        }












        public static String PlainText(String e)
        {
            e = System.Text.RegularExpressions.Regex.Replace(e, "<(.|\n)*?>", String.Empty);
            e = e.Replace("&quot;", "\"");
            e = e.Replace("&amp;", "&");
            e = e.Replace("&#39;", "'");
            e = e.Replace("&pound;", "£");
            e = e.Replace("%20", " ");
            e = e.Replace("%C3%A9", "é");

            return e;
        }

        public static List<String> LongTextParse(String e)
        {
            List<String> mStrings = new List<string>();
            int count = System.Text.RegularExpressions.Regex.Matches(e, "\n").Count + 1;

            // Set Up Biography
            String bioworkstring = e;
            int x = 0;
            Boolean empty = false;

            while ((x < count) && (empty == false))
            {
                String s;
                // Get Paragraph
                if (bioworkstring.IndexOf("\n", 1) > 0)
                {
                    // Get Leading Paragraph
                    s = bioworkstring.Remove(bioworkstring.IndexOf("\n", 0));
                    if (s.StartsWith(" ") && s.Length > 2)
                    {
                        s = s.Remove(0, 1);
                    }
                }
                else
                {
                    // Add Final Paragraph
                    s = bioworkstring;
                    // Finished!
                    empty = true;
                }
                // Add Paragraph to Stackpanel
                mStrings.Add(s);
                // Remove Leading Paragrpah
                bioworkstring = bioworkstring.Remove(0, (bioworkstring.IndexOf("\n", 1) + 1));
                // Increment Counter
                x = (x + 1);
            }
            mStrings.Add(" ");

            return mStrings;
        }
    }
}
